package com.cb.kfc.controller;

import com.cb.kfc.domain.Orders;
import com.cb.kfc.service.IOrdersService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;
//查询全部订单【未分页】
//    @RequestMapping("/findAll.do")
//    public ModelAndView findAll() throws Exception{
//        ModelAndView mv = new ModelAndView();
//        List<Orders> ordersList = ordersService.findAll();
//        mv.addObject("ordersList",ordersList);
//        mv.setViewName("orders-page-list");
//        return mv;
//    }

    //查询全部订单【已分页】
    @RequestMapping("/findAll.do")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page,@RequestParam(name = "size",required = true,defaultValue = "4")Integer size) throws Exception{
        ModelAndView mv = new ModelAndView();
        List<Orders> ordersList = ordersService.findAll(page,size);
        PageInfo pageInfo = new PageInfo(ordersList);
        mv.addObject("pageInfo",pageInfo);
        mv.setViewName("orders-page-list");
        return mv;
    }


    //订单添加
    @RequestMapping("/save.do")
    public String save(Orders orders) throws Exception {
        ordersService.save(orders);
        return "redirect:findAll.do";
    }

    /**
     * 删除订单
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete.do")
    public String delete(String[] ids) throws Exception{
        Integer  integer = ordersService.deleteProductById(ids);
        return "redirect:findAll.do";
    }

    /**
     *展示详情的功能
     * @return
     * @throws Exception
     */
    @RequestMapping("/findById.do")
    public ModelAndView findById(@RequestParam(name="id",required = true) String id) throws Exception{
        ModelAndView mv = new ModelAndView();
        Orders orders = ordersService.findById(id);
        orders.toString();
        mv.addObject("orders",orders);
        mv.setViewName("orders-show");
        return mv;
    }
    @RequestMapping("/order-add.do")
    public ModelAndView orderAdd() throws Exception{
        ModelAndView mv = new ModelAndView();
        mv.setViewName("order-add");
        return mv;
    }
}
